package dimension.starry.player;

import dimension.starry.Tasks.PlayerTaskManager;
import dimension.starry.constants.PlayerDataType;
import dimension.starry.damage.DamageTypes;
import dimension.starry.dataManager.PlayerDataManager;
import dimension.starry.effect.Effects;
import net.minecraft.server.network.ServerPlayerEntity;

/**
 * @author 150149
 */
public class WaterDecrease {

    public static PlayerTaskManager.PlayerTask getWaterDecreaseTask() {
        return player -> {
            int water = PlayerDataManager.getInt(player, PlayerDataType.WATER);

            if (water > 0 && !player.isCreative()) {
                if (player.hasStatusEffect(Effects.THIRSTY.getStatusEffect())) {
                    PlayerDataManager.setServerPlayerData(player, PlayerDataType.WATER, water - 300);
                } else {
                    PlayerDataManager.setServerPlayerData(player, PlayerDataType.WATER, water - 3);
                }
            } else if (water < 0) {
                PlayerDataManager.setServerPlayerData(player, PlayerDataType.WATER, 0);
            } else if (player.isAlive() && !player.isInvulnerable()) {
                player.damage(DamageTypes.create(player.getWorld(), DamageTypes.THIRSTY.getDamageType()), player.getMaxHealth() / 10);
            }
        };
    }

    public static void onPlayerRespawn(ServerPlayerEntity oldPlayer, ServerPlayerEntity newPlayer, boolean alive) {
        PlayerDataManager.setServerPlayerData(newPlayer, PlayerDataType.WATER, 10000);
    }

}
